<?php

namespace App\Model\Common;

class AdminLog extends Common
{
    public static function log($request, $model = null, $opt = [])
    {
        $data = [];
        $rid = $request->input('id') ?: 0;
        if ($model && !$rid) {
            if (isset($model->id) && !empty($model->id)) {
                $rid = $model->id;
            }
        }
        $data['rid'] = $rid;
        // $data['rid'] = $request->input('id') ?: isset($model->id) ? $model->id : 0;
        $data['admin_id'] = session('admin:aid');
        $data['admin_signname'] = session('admin:signname');
        $url = $request->getPathInfo();
        $url = substr($url, 1, strlen($url));
        $data['url'] = $url;
        $data['ip'] = $request->ip();

        $Menu = new AdminMenu();
        $menus = $Menu->getOriginMenus();
        foreach ($menus as $menu) {
            if ($menu['url'] == $url) {
                $data['title'] = $menu['title'];
            }
        }

        if (!isset($data['title'])) {
            $data['title'] = '未知操作';
        }

        $data['request'] = json_encode($request->except(['_token']));
        self::insert($data);

        return true;
    }
}
